/*!
 *  Copyright (C) 2011  Anthony Wertz.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "StatGen.h"

//------------------------------------------------------------------------------

bool StatGen(
  const GradeType * const grades,
  const unsigned int numberOfGrades,
  unsigned int & numberOfStudentsAtOrBelowAverage,
  unsigned int & numberOfStudentsAboveAverage )
{
  bool result = false;
  
  // Valid list?
  if ( grades )
  {
    // Empty?
    if ( 0 == numberOfGrades )
    {
      numberOfStudentsAtOrBelowAverage = 0;
      numberOfStudentsAboveAverage = 0;
    }
    
    // Only one student?
    else if ( 1 == numberOfGrades )
    {
      numberOfStudentsAtOrBelowAverage = 1;
      numberOfStudentsAboveAverage = 0;
    }
    
    // Multiple students.
    else
    {
      double sampleMean = 0.0;
      
      // Iterate through the list to compute the sum of all grades, then divide
      // by the number of grades to get the sample mean.
      for ( unsigned int i = 0; i < numberOfGrades; ++i )
      {
        sampleMean += grades[i].grade;
      }
      sampleMean /= numberOfGrades;
      
      // Iterate through again to determine the number of students above and
      // below the mean.
      numberOfStudentsAtOrBelowAverage = 0;
      numberOfStudentsAboveAverage = 0;
      for ( unsigned int i = 0; i < numberOfGrades; ++i )
      {
        // Above average?
        if ( sampleMean < grades[i].grade )
        {
          ++numberOfStudentsAboveAverage;
        }
        
        // At or below average.
        else
        {
          ++numberOfStudentsAtOrBelowAverage;
        }
      }
    }
    
    // Valid list was analyzed.
    result = true;
  }
  
  // Done.
  return result;
}

//------------------------------------------------------------------------------

